package com.demo.nio;

import com.demo.nio.thread.BossThread;
import com.demo.nio.thread.WorkThread;

import java.io.IOException;
import java.nio.channels.Selector;

/**
 * 服务端
 */
public class NIOServer {

    private int port = 8080;

    public NIOServer(int port) {
        this.port = port;
    }

    /**
     * 服务器运行
     *
     * @throws IOException
     */
    public void run() throws IOException {
        /**
         * 轮询是否有新的连接
         */
        Selector bossSelector = Selector.open();

        /**
         * 负责轮询连接是否有数据可读
         */
        Selector workSelector = Selector.open();

        BossThread bossThread = new BossThread(bossSelector, workSelector, port);
        WorkThread workThread = new WorkThread(workSelector);

        bossThread.start();
        workThread.start();
    }

    public static void main(String[] args) throws IOException {
        NIOServer nioServer = new NIOServer(8000);
        nioServer.run();
    }
}
